---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: nacos
  name: nacos
spec:
  # 指定serviceName
  serviceName: nacos
  # Parallel: 并行启动pod
  podManagementPolicy: Parallel
  replicas: 1
  selector:
    matchLabels:
      app: nacos
  template:
    metadata:
      labels:
        app: nacos
    spec:
      containers:
      - env:
        # 镜像的环境变量指定使用mysql
        - name: SPRING_DATASOURCE_PLATFORM
          value: mysql
        # 使用主机,因为k8s ip会变
        - name: PREFER_HOST_MODE
          value: hostname
        # 模式:单实例
        - name: MODE
          value: standalone
        # 暴露metrics数据: /nacos/actuator/prometheus
        - name: MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE
          value: "*"
        # 打开tomcat日志
        - name: TOMCAT_ACCESSLOG_ENABLED
          value: "true"
        # 数据库信息
        - name: MYSQL_SERVICE_HOST
          valueFrom:
            configMapKeyRef:
              name: nacos-cm
              key: mysql.host
        - name: MYSQL_SERVICE_DB_NAME
          valueFrom:
            configMapKeyRef:
              name: nacos-cm
              key: mysql.db.name
        - name: MYSQL_SERVICE_PORT
          valueFrom:
            configMapKeyRef:
              name: nacos-cm
              key: mysql.port
        - name: MYSQL_SERVICE_USER
          valueFrom:
            configMapKeyRef:
              name: nacos-cm
              key: mysql.user
        - name: MYSQL_SERVICE_PASSWORD
          valueFrom:
            configMapKeyRef:
              name: nacos-cm
              key: mysql.password
        image: {{ image }}
        imagePullPolicy: IfNotPresent
        name: nacos
        ports:
        - containerPort: 8848
          name: server
          protocol: TCP
        - containerPort: 9848
          name: client-rpc
          protocol: TCP
        - containerPort: 9849
          name: raft-rpc
          protocol: TCP
        # 资源限制
        resources:
          limits:
            cpu: 2
            memory: 2Gi
          requests:
            cpu: 100m
            memory: 512Mi
        volumeMounts:
        - mountPath: /home/nacos/logs
          name: nacos-logs
      restartPolicy: Always
      volumes:
      - name: nacos-logs
        nfs:
          path: {{ nfs_path }}
          server: {{ nfs_host }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
data:
  mysql.host: "{{ mysql_host }}"
  mysql.db.name: "nacos"
  mysql.port: "3306"
  mysql.user: "{{ mysql_user }}"
  mysql.password: "{{ mysql_password }}"
---
apiVersion: v1
kind: Service
metadata:
  name: nacos
  labels:
    app: nacos
spec:
  type: NodePort
  # Pod 未就绪也发布Endpoint
  publishNotReadyAddresses: true
  ports:
  - port: 8848
    name: server
    targetPort: 8848
    nodePort: 31848
  - port: 9848
    name: client-rpc
    targetPort: 9848
    nodePort: 31948
  - port: 9849
    name: raft-rpc
    targetPort: 9849
    nodePort: 31949
  selector:
    app: nacos